<template>
  <view class="mobile-item-container">
    <Navbar :title="id ? '修改' : '新增'" bgColor="#fff" :h5Show="false"></Navbar>
	<uni-section :title="id ? '修改' : '新增'" type="line">
		<view class="example">
			<uni-forms ref="form" :model="formData" :rules="rules" labelWidth="90px">
				<uni-forms-item label="水果名称" required name="fruitsName">
					<uni-easyinput v-model="formData.fruitsName" placeholder="请输入水果名称" />
				</uni-forms-item>
				<uni-forms-item label="单位" required name="unit">
					<uni-data-select v-model="formData.unit" :localdata="units" />
				</uni-forms-item>
				
				<uni-forms-item label="进货价" required name="purchasePrice">
					<uni-easyinput v-model="formData.purchasePrice" placeholder="请输入进货价" />
				</uni-forms-item>
				
				<uni-forms-item label="零售价" required name="retailPrice">
					<uni-easyinput v-model="formData.retailPrice" placeholder="请输入零售价" />
				</uni-forms-item>
				
				<uni-forms-item label="库存总量"  name="inventoryTotal">
					<uni-easyinput v-model="formData.inventoryTotal" disabled />
				</uni-forms-item>
				<uni-forms-item label="有效库存量"  name="inventory">
					<uni-easyinput v-model="formData.inventory" disabled />
				</uni-forms-item>
				<uni-forms-item label="损耗库存量"  name="lossNum">
					<uni-easyinput v-model="formData.lossNum" disabled/>
				</uni-forms-item>
				<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<u-row :gutter="12">
				  <u-col :span="4">
				    <u-button v-if="id" type="error" text="删除" @click="del" v-hasPermi="['fruits:fruitsInfo:remove']"></u-button>
				  </u-col>
				  <u-col :span="4">
				    <u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
				  </u-col>
				  <u-col :span="4">
					<u-button v-if="!id" type="primary" text="新增" @click="submit" v-hasPermi="['fruits:fruitsInfo:add']"></u-button>
				    <u-button  v-else type="primary" text="修改" @click="submit" v-hasPermi="['fruits:fruitsInfo:edit']"></u-button>
				  </u-col>
				</u-row>
			</view>
		</view>
	</uni-section>
	<buttom-tabbar></buttom-tabbar>
  </view>
</template>

<script>
import * as FruitsInfoApi from '@/api/fruits/fruitsInfo'
import * as DictInfoApi from '@/api/system/dict/data'
import Navbar from '@/components/navbar/Navbar'

export default {
  components: {
    Navbar,
  },
  data () {
    return {
      id: undefined,
      formData:{
              id: null,
			  fruitsName: null,
			  unit: null,
			  purchasePrice: null,
			  retailPrice: null,
			  inventory: null,
			  createBy: null,
			  createTime: null,
			  updateBy: null,
			  updateTime: null,
			  remark: null
        },
	  actions: [],
	  actionShow: false,
	  nickName:"",
	  actionTitle: '',
	  // 校验规则
	  rules: {
	  	fruitsName: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '水果名称不能为空'
	  		}]
	  	},
		unit: {
			rules: [{
				required: true,
				errorMessage: '单位不能为空'
			}]
		},
	  	purchasePrice: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '进货价不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '进货价只能输入数字'
	  		}]
	  	},
		retailPrice: {
			rules: [{
				required: true,
				errorMessage: '零售价不能为空'
			}, {
				format: 'number',
				errorMessage: '零售价只能输入数字'
			}]
		},
	  },
	  showCalendar:false,
	  // 单选数据源
	  units: [],
    }
  },

  onLoad (params) {
    this.id = params.id
    this.loadData()
	DictInfoApi.getDicts("unit").then(res => {
		if(res.data)
		{
			if(res.data.length)
			{
				this.units=[]
				for(let i=0;i<res.data.length;i++)
				{
					let temp = res.data[i];
					let obj = {};
					obj.value = temp.dictValue;
					obj.text = temp.dictLabel
					this.units.push(obj);
				}
			}
		}
	});
  },
  methods: {
    loadData () {
      if (this.id) {
        const app = this
        FruitsInfoApi.getFruitsInfo(this.id).then(res => {
			if(res.data)
			{
				app.formData = res.data
			}
        })
      }
    },
    del() {
		let id = this.id
		uni.showModal({
			title: '提示',
			content: '确定要删除吗？',
			success: function (res) {
				if (res.confirm) {
					FruitsInfoApi.delFruitsInfo(id).then(res2 => {
					  uni.showToast({ title: '提交成功！' })
					  uni.navigateTo({ url: '/pages/fruits/fruitsInfo/index'})
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
    },
    submit() {
		this.$refs.form.validate().then(res => {
		  if (this.id) {
		    FruitsInfoApi.updateFruitsInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  } else {
		    FruitsInfoApi.addFruitsInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  }
		});
    },
    
    goBack() {
		uni.navigateTo({ url: '/pages/fruits/fruitsInfo/index'})
    },
  }
}
</script>
<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
